<?php

/******************************************
 * File:			  lib_imports.php
 * Version:			1.0.2
 * Last modified:	30/12/2009
 * Last modifier:	Hugo Lavigne
 *****************************************/

Function imports_css($css, $browser="FF"){
	GLOBAL $PWerr;
	
	$msg = "Impossible d'importer la librairie CSS \"$css\"";
	$tab_css = explode('.', $css);
	$nb = count($tab_css);
	
  	if ($tab_css[0] == 'odt_frm_php'){
    	if ($nb == 2){
    		$path = '../'.$_SESSION['path_css'].'/'.$tab_css[1].'.css';
    		if (!file_exists($path))
    			$path = $_SESSION['path_css'].'/'.$tab_css[1].'.css';
    		if (!file_exists($path)){
    			$PWerr->sWarning($msg, 175);
    			return (-1);
    		}
			if ($browser == 'IE')
				echo "<!--[if IE]>";
			echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"$path\" />";
			if ($browser == 'IE')
				echo "<![endif]-->";

		}
		elseif ($nb == 3){
			$path = '../'.$_SESSION['path_css'].'/'.$tab_css[1].'/'.$tab_css[2].'.css';
			if (!file_exists($path))
				$path = $_SESSION['path_css'].'/'.$tab_css[1].'/'.$tab_css[2].'.css';
			if (!file_exists($path)){
				$PWerr->sWarning($msg, 176);
				return (-1);
			}
			if ($browser == 'IE')
				echo "<!--[if IE]>";
			echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"$path\" />";
			if ($browser == 'IE')
				echo "<![endif]-->";
		}
	}
	elseif ($tab_css[0] == 'currentt'){
		$path = $_SESSION['path_templates'].'/'.$_SESSION['template'].'/css/'.$tab_css[1].'.css';
		if (!file_exists($path)){
			$path = 'templates/'.$_SESSION['template_admin'].'/css/'.$tab_css[1].'.css';
			if (!file_exists($path)){
				$PWerr->sWarning($msg, 177);
				exit();
			}
		}
		if ($browser == 'IE')
			echo "<!--[if IE]>";
		echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"$path\" />";
		if ($browser == 'IE')
			echo "<![endif]-->";
	}
	elseif ($tab_css[0] == 'currenta'){
		$path = $_SESSION['current_path'].'/template/css/'.$tab_css[1].'.css';
		if ($browser == 'IE')
			echo "<!--[if IE]>";
		echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"$path\" />";
		if ($browser == 'IE')
			echo "<![endif]-->";
	}
	else{
		if ($nb == 2)
			$path = $_SESSION['path_apps'].'/'.$tab_css[0].'/external/css/'.$tab_css[1].'.css';
		elseif ($nb == 1)
			$path = $_SESSION['current_path'].'/external/css/'.$tab_css[0].'.css';
		else{
			$PWerr->sWarning($msg, 178);
			return (-1);
		}
		if (!file_exists($path)){
			$PWerr->sWarning($msg, 179);
			return (-1);
		}
		if ($browser == 'IE')
			echo "<!--[if IE]>";
		echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"$path\" />";
		if ($browser == 'IE')
			echo "<![endif]-->";
	}
	return (0);
}

Function imports_js($js){
	GLOBAL $PWerr;
	
	$msg = "Impossible d'importer la librairie JavaScript \"$js\"";
	$tab_js = explode('.', $js);
	$nb = count($tab_js);
  	if ($tab_js[0] == 'odt_frm_php'){
    	if ($nb == 2){
    		$path = '../'.$_SESSION['path_js'].'/'.$tab_js[1].'/';
			$files = get_file_list($path);
			if (!is_array($files)){
				$path = $_SESSION['path_js'].'/'.$tab_js[1].'/';
				$files = get_file_list($path);
				if (!is_array($files)){
					$PWerr->sWarning($msg, 180);
					return (-1);
				}
			}
			$nb = count($files);
			$i = 0;
			while ($i < $nb){
				$file = $path.$files[$i];
				echo "<script src=\"$file\" type=\"text/javascript\"></script>";
				$i++;
			}
		}
		elseif ($nb == 3){
			$path = $_SESSION['path_js'].'/'.$tab_js[1].'/'.$tab_js[2].'.js';
			if (!file_exists($path))
				$path = $_SESSION['path_js'].'/'.$tab_js[1].'/'.$tab_js[2].'.js';
			if (!file_exists($path)){
				$PWerr->sWarning($msg, 182);
				return (-1);
			}
			echo "<script src=\"$path\" type=\"text/javascript\"></script>";
		}
	}
	else{
		if ($nb >= 2){
			$path = $_SESSION['path_apps'].'/'.$tab_js[0].'/external/js/';
			$i = 1;
			
			while ($i < $nb){
				if (($i + 1) == $nb)
					$path .= $tab_js[$i].'.js';
				else
					$path .= $tab_js[$i].'/';
				$i++;
			}
			echo "<script src=\"$path\" type=\"text/javascript\"></script>";
		}
		elseif ($nb == 1){
			$path = '../'.$_SESSION['path_apps'].'/'.$tab_js[0].'/external/js/';
			$files = get_file_list($path);
			if (!is_array($files)){
				$path = $_SESSION['path_apps'].'/'.$tab_js[0].'/external/js/';
				$files = get_file_list($path);
				if (!is_array($files)){
					$PWerr->sWarning($msg, 190);
					return (-1);
				}
			}
			$nb = count($files);
			$i = 0;
			while ($i < $nb){
				$file = $path.$files[$i];
				echo "<script src=\"$file\" type=\"text/javascript\"></script>";
				$i++;
			}
		}
		else{
			$PWerr->sWarning($msg, 183);
			return (-1);
		}
	}
	return (0);
}

Function imports($api){
	GLOBAL $PWerr;
	
	$msg = "Impossible d'importer la librairie PHP \"$api\"";
	$tab_api = explode('.', $api);
	$nb = count($tab_api);
	
	if ($tab_api[0] == 'odt_frm_php'){
		$path = '../'.$_SESSION['path_php'].'/lib_'.$tab_api[1].'.inc';
		if (!file_exists($path)){
			$path = $_SESSION['path_php'].'/lib_'.$tab_api[1].'.inc';
			if (!file_exists($path)){
				$path = '../'.$_SESSION['path_php'].'/lib_'.$tab_api[1].'.php';
				if (!file_exists($path)){
					$path = $_SESSION['path_php'].'/lib_'.$tab_api[1].'.php';
					if (!file_exists($path)){
						$PWerr->sWarning($msg, 185);
						return (-1);
					}
				}
			}
		}
		include_once ($path);
		return (0);
	}
	elseif ($tab_api[0] == 'webadmin'){
		if ($nb == 2){
			$path = $_SESSION['path_webadmin'].'/'.$_SESSION['path_apps'].'/'.$tab_api[1].'/functions/';
			$files = get_file_list($path);
			if (!is_array($files)){
				$PWerr->sWarning($msg, 186);
				return (-1);
			}
			$nb = count($files);
			$i = 0;
			while ($i < $nb){
				include_once ($path.$files[$i]);
				$i++;
			}
			return (0);
		}
		elseif ($nb > 2){
			$path = $_SESSION['path_webadmin'].'/'.$_SESSION['path_apps'].'/'.$tab_api[1].'/functions/';
			$i = 2;
			while ($i < $nb){
				if (($i + 1) == $nb){
					$path .= $tab_api[$i];
					$path_inc = $path.'.inc';
					$path_php = $path.'.php';
					
				}
				else
					$path .= $tab_api[$i].'/';
				$i++;
			}
		}
		else{
			$PWerr->sWarning($msg, 191);
			return (-1);
		}
		
		if (!file_exists($path_inc)){
			$path_inc = '../'.$path_inc;
			if (!file_exists($path_inc)){
				if (!file_exists($path_php)){
					$path = '../'.$path_php;
					if (!file_exists($path_php)){
						$PWerr->sWarning($msg, 187);
						return (-1);
					}
					else
						$path = $path_php;
				}
				else
					$path = $path_php;
			}
			else
				$path = $path_inc;
		}
		else
			$path = $path_inc;
		include_once ($path);
		return (0);
	}
	else{
		if ($nb == 1){
			$path = $_SESSION['path_apps'].'/'.$tab_api[0].'/functions/';
			$files = get_file_list($path);
			if (!is_array($files)){
				$PWerr->sWarning($msg, 188);
				return (-1);
			}
			$nb = count($files);
			$i = 0;
			while ($i < $nb){
				include_once ($path.$files[$i]);
				$i++;
			}
			return (0);
		}
		elseif ($nb >= 2){
			$path = $_SESSION['path_apps'].'/'.$tab_api[0].'/functions/';
			$i = 1;
			while ($i < $nb){
				if (($i + 1) == $nb){
					$path .= $tab_api[$i];
					$path_inc = $path.'.inc';
					$path_php = $path.'.php';
				}
				else
					$path .= $tab_api[$i].'/';
				$i++;
			}
			if (!file_exists($path_inc)){
				$path_inc = '../'.$path_inc;
				if (!file_exists($path_inc)){
					if (!file_exists($path_php)){
						$path_php = '../'.$path_php;
						if (!file_exists($path_php)){
							$PWerr->sWarning($msg, 189);
							return (-1);
						}
						else
							$path = $path_php;
					}
					else
						$path = $path_php;
				}
				else
					$path = $path_inc;
			}
			else
				$path = $path_inc;
			include_once ($path);
			return (0);
		}
		else{
			$PWerr->sWarning($msg, 181);
			return (-1);
		}
	}
}

Function page($page){
	GLOBAL $PWerr;
	
	$msg = "Impossible d'importer la page \"$page\"";
	$tab_page = explode('.', $page);
	$nb = count($tab_page);
	
	$i = 0;
	
	if ($nb >= 2){
		$path = $_SESSION['path_apps'].'/';
		while ($i < $nb){
			if (($i + 1) == $nb)
				$pah .= $tab_page[$i].'.php';
			else
				$pah .= $tab_page[$i].'/';
		}
	}
	elseif ($nb == 1){
		$path = $_SESSION['current_path'].'/'.$tab_page[0].'.php';
	}
	else{
		$PWerr->sWarning($msg, 173);
		return (-1);
	}
	
	if (!file_exists($path)){
		$PWerr->sWarning($msg, 174);
		return (-1);
	}
	
	include_once ($path);
	
	return (0);
}

?>